namespace two {
    export interface IChangeGoodsData {
        id: number,
        count: number,
        sqlId: number
    }

    function getProxyClass(apiName: string): any {
        var clazz = egret.getDefinitionByName(apiName + "Proxy");

        return new clazz();
    }

    export class BaseProxy {
        private apiName: string;

        public needSend: boolean = true;
        public urlType: number = 0;

        protected responseKeys: string[] = [];

        constructor() {
            let className = egret.getQualifiedClassName(this);

            this.apiName = className.substring(4, className.length - 5);
        }

        /**
         * 通知前处理
         */
        protected responseBefore(): void {

        }

        /**
         * 通知后处理
         */
        private responseAfter(): void {
        }

        private requestProxyKey: string;
        private requestCode: number;
        protected register(request: string, requestCode: number): void {
            this.requestProxyKey = request;
            this.requestCode = requestCode;
        }

        private proxyData: any = {};//当前请求数据，如果一个proxy多次调用，data会覆盖

        protected sendData: any;

        private call?: (data) => void;
        private needMsg: boolean = false;

        send(data: any, call?: (data: { code: number, data: any }) => void, needMsg?: boolean) {
            this.sendData = data;

            this.call = call;

            this.needMsg = (needMsg !== false);

            if (this.needSend) {
                HttpOperator.Instance().onSendCommonRequest(this.apiName, data, this.onResponse.bind(this));
            }
            else {
                one.callLater(() => {
                    this.onResponse({ code: 0, msg: "", data: {} });
                }, 1);
            }
        }

        getRequest(): any {
            return this.sendData;
        }

        getResponse(): any {
            return this.proxyData;
        }


        private apis: string[] = []
        protected addRefreshApi(apiName: string): void {
            if (this.apis.indexOf(apiName) < 0) {
                this.apis.push(apiName);
            }
        }

        private onResponse(respondData: { code: number, msg: string, data: any }): void {
            if (respondData.code == 0) {//成功
                this.proxyData = respondData.data;
                this.apis = [this.apiName];
                this.responseBefore();
                let apis = this.apis;
                for (let i: number = 0; i < apis.length; i++) {
                    two.onRefreshApi(apis[i]);
                }
                this.responseAfter();
            }
            else if (this.needMsg) {
                // let errorCodes = RES.getRes("allErrorCodes_json")['codes'];
                // if (errorCodes[respondCode * 100 + respondData.result]) {
                //     // two.showTip(errorCodes[respondCode * 100 + respondData.result]);
                // }
            }

            if (this.call) {
                let call = this.call;
                this.call = null;
                call({ code: respondData.code, data: this.proxyData, msg: respondData.msg });
            }
        }
    }
}